/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhelper.blls;

import JhelperModels.AddressBookBean;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Administrator
 */
public class AddressBook {

   
    private static class newcard {

        private static String getName() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getMobile() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getOffice() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getHome() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getQq() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getEmail() {
            throw new UnsupportedOperationException("Not yet implemented");
        }

        private static String getOther() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
       
        public newcard() {
        }
    }
     
    
    
    public boolean newCard(AddressBookBean newCard){
        //把bewCard通讯录对象存起来
        Document doc;
        Element addressbook,card;
        Element name,mobile,office,home,qq,email,other;
        boolean flag=false;
        
       try{
         DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
         DocumentBuilder db=f.newDocumentBuilder();
         
         File file=new File("addressbook");
         if(! file.exists()){
             file.mkdir();
         }
         file=new File("addressbook/addressbook.xml");
         if(! file.exists()){
             flag=true;
             file.createNewFile();
             doc=db.newDocument();
             addressbook=doc.createElement("addressbook");
         }else{
             doc=db.parse(file);
             addressbook=(Element) doc.getElementsByTagName("addressbook").item(0);
          }
         //构建card元素，它是name等元素的父元素
         card=doc.createElement("card");
         name=doc.createElement("name");
         mobile=doc.createElement("mobile");
         office=doc.createElement("office");
         home=doc.createElement("home");
         qq=doc.createElement("qq");
         email=doc.createElement(" email");
         other=doc.createElement("other");
         
         //为name等元素添加文本节点内容，内容来自于
         name.appendChild(doc.createTextNode(newcard.getName()));
         mobile.appendChild(doc.createTextNode(newcard.getMobile()));
         office.appendChild(doc.createTextNode(newcard.getOffice()));
         home.appendChild(doc.createTextNode(newcard.getHome()));
         qq.appendChild(doc.createTextNode(newcard.getQq()));
         email.appendChild(doc.createTextNode(newcard.getEmail()));
         other.appendChild(doc.createTextNode(newcard.getOther()));
         
        
        
         
        addressbook.appendChild(card);
        if(flag){
            doc.adoptNode(addressbook);
            
        }
          
         
         //各元素之间的关系确定以后，需要dom转换成xml
        TransformerFactory ff=TransformerFactory.newInstance();
        Transformer t=ff.new Transformer();
        
        
         FileWriter fout=new FileWriter(file);
         t.transform(new DOMSource(doc),new StreamResult(fout));
         return true;
             
        
         
         
         
       
       }catch(Exception e){
           e.printStackTrace();
           return false;
           }  
 
     
        
        
        
        
        
        
        
        
        
        
     
        
        
    }
    public boolean modifyCard(AddressBookBean oldcard) throws ParserConfigurationException, IOException, TransformerException{
       
        Document doc;
        NodeList cards;
        Element  addressbook,card,newcard,oldname;
        Element name,mobile,office,home,qq,email,other;
        try{
            
            DocumentBuilderFactory f = new DocumentBuilderFactory.newInstance();
            DocumentBuilder db=f.newDocumentBuilder();
            
            File file=new File("addressbook/addressbook.xml");
            if(!file.exists()){
              return false;
              
            }
            doc=db.newDocument();
            addressbook=(Element)doc.getElementsByTagName("addressbook").item(0);
            
            
            cards = doc.getElementsByTagName("card");
            
            for(int i=0;i<cards.getLength();i++){
                card=(Element)cards.item(i);
                oldname=(Element)card.getElementsByTagName("name").item(0);
                if(oldname.getFirstChild().getNodeValue().equals(oldcard.getName())){
                  
                        newcard=doc.createElement("card");
                        card=doc.createElement("card");
                        name=doc.createElement("name");
                        mobile=doc.createElement("mobile");
                        office=doc.createElement("office");
                        home=doc.createElement("home");
                        qq=doc.createElement("qq");
                        email=doc.createElement(" email");
                        other=doc.createElement("other");
                        
                        name.appendChild(doc.createTextNode(oldcard.getName()));
                        mobile.appendChild(doc.createTextNode(oldcard.getMobile()));
                        office.appendChild(doc.createTextNode(oldcard.getOffice()));
                        home.appendChild(doc.createTextNode(oldcard.getHome()));
                        qq.appendChild(doc.createTextNode(oldcard.getQq()));
                        email.appendChild(doc.createTextNode(oldcard.getEmail()));
                        other.appendChild(doc.createTextNode(oldcard.getOther()));
         
                         newcard.appendChild(name);
                         newcard.appendChild(mobile);
                         newcard.appendChild(office);
                         newcard.appendChild(home);
                         newcard.appendChild(qq);
                         newcard.appendChild(email);
                         newcard.appendChild(other); 
                         
                         
                         
                          doc.replaceChild(newcard, card);
                          TransformerFactory ff=TransformerFactory.newInstance();
                          Transformer t=ff.new Transformer();
        
        
                          FileWriter fout=new FileWriter(file);
                          t.transform(new DOMSource(doc),new StreamResult(fout));
                          return true;
                          
                    
                }
                
            }
            return false;
            
        
        
    
        } 
    }
    
    public boolean delCard(String name){
        
        
        return true;
    }
    public Hashtable getAllCard(){
        
        return null;
    }
    
    
    public static void main(String[] args) {
        
        AddressBookBean  newcard=new AddressBookBean("张三""13131131313","07711254646","077141654646","45564135494");
        AddressBook ab =  new AddressBook();
        
        //            ab.newCard(newcard);
    
    
    
}
